package com.cy.utils;

/**
 * Created by Administrator on 13-12-20.
 */
public final class RandomCodeGenerator {

    private final static String[] CODES = {"0","1","2","3","4","5","6","7","8","9",
            "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    public static int generateCheckCode(int n) {
        int factor = (int) Math.pow(10, n - 1);
        int random = (int) ((Math.random() * 9 + 1) * factor);
        return random;
    }

    public static String generateCheckCodeString(int n) {
        return Integer.toString(RandomCodeGenerator.generateCheckCode(n));
    }

    public static String randomNumberAndLetter(int length){
       /* StringBuilder sb = new StringBuilder();
        for(int i = 0 ;i < lenght;i++ ){
            int random = (int) Math.round(Math.random()*35);
            sb.append(codes[random]);
        }
*/
        return randomNumberAndLetter(length, CODES.length);
    }

    /**
     * 数字的随机码
     * @param length  随机码的长度
     * @return 随机码
     */
    public static String randomNumber(int length){
        return randomNumberAndLetter(length, 10);
    }

    /**
     * 数字与字母结合的随机码
     * @param length  随机码的长度
     * @param codeLength    内容长度
     * @return 随机码
     */
    public static String randomNumberAndLetter(int length,int codeLength){
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ;i < length;i++ ){
            int random = (int) Math.round(Math.random()*(codeLength-1));
            sb.append(CODES[random]);
        }

        return sb.toString();
    }

    public static void main(String[] args){
        String code = RandomCodeGenerator.randomNumber(5);
        System.out.println(code);
    }
}
